<?php
require_once(Kohana::find_file('vendor','phprpc/phprpc_server'));
/**
 * Created by PhpStorm.
 * User: locove
 * Date: 14-10-9
 * Time: 上午9:54
 * Note: 为供应商系统提供数据的服务端
 */
class Controller_Purchaseapi extends Controller
{
    protected $server;
    protected $url;
    protected $key;
    public function before(){
        $this->server=new PHPRPC_Server();
        $this->url = 'http://dev.prodistsys.kokoerp.com';
        $this->key = '6a0ec862b88a5ea336d10a6d9eaa2c89';
        include_once(MODPATH.'/commonapi/classes/purchaseApi.php');
        parent::before();
    }

    /**
     * 测试
     */
    public function action_index(){
//        $aa = PurchaseApi::purchasePull(16961);
        $aa = PurchaseApi::supplierReturnsPull(83);
        echo "<pre>";print_r($aa);echo "</pre>";die;
        $params = array(
            'notId' =>'26961',
            'logistics' =>'EMS',
            'logisticsName' =>'顺风',
            'trackNo' =>'123456789',
            'arrivalTime' =>'2018-02-11 18:00:00',
            'data' =>array(
                'subjectSku'=>'A301',
                'unifiedSku'=>'A301',
                'deliverNum'=>'20',
                'remark'=>'顺风'
            )
        );
        $key = '52275b50893f4d33bde2dfaa44144735';
        $data['data'] = json_encode($params);
        $md5 = md5($data['data'].$key);
        $data['sign'] = $md5;
//        $host = 'http://192.168.201.38:803/commonapi/purchaseApi/purchaseGet';
        $host = 'http://dev.yksvmi.kokoerp.com/commonapi/purchaseapi/purchaseGet';
        $aa = PurchaseApi::post_curl($host,$data);
        echo "<pre>";print_r($aa);echo "</pre>";die;
    }
    /**
     *  预约单推送接口
     *  作者:kelvin
     * */
    public function action_purchasePull($ids = array())
    {
        $data = PurchaseApi::purchasePull($ids);
        if(!$ids){
            return array('code'=>400,'msg'=>'参数错误');die;
        }
        $url = $this->url.'/api/index.php?c=vmi&a=setOrder';
        $msg = array();
        foreach ($data as $v){
            $result = PurchaseApi::post_curl($url,$v,$this->key);
            if($result['code']==200){
                continue;
            }else{
                $msg[] = $v['notId'];
            }
        }
        return $msg;
    }
    /**
     *  预约单接收接口
     *  作者:kelvin
     * */
    public function action_purchaseGet()
    {
        $data = $_POST?$_POST:$_GET;
        if(PurchaseApi::check_sign($data)==false){
            echo json_encode(array('code'=>303,'msg'=>'签名验证失败'));die;
        }else{
            if($data['data']){
                $result = PurchaseApi::purchaseLogisticsGet(json_decode($data['data'],true));
                echo json_encode($result);die;
            }else{
                echo json_encode(array('code'=>300,'msg'=>'参数不存在'));die;
            }
        }

    }
    /**
     *  推送退货
     *  作者:kelvin
     * */
    public function action_supplierReturnsPull($ids)
    {
        $data = PurchaseApi::supplierReturnsPull($ids);
        if(!$ids){
            return array('code'=>400,'msg'=>'参数错误');
        }
        $url = $this->url.'/api/index.php?c=vmi&a=returnSku&md5=';
        $msg = array();
        foreach ($data as $v){
            $result = PurchaseApi::post_curl($url,$v);
            if($result['code']==200){
                continue;
            }else{
                $msg[] = $v['notId'];
            }
        }
        return $msg;
    }
    /**
     *  接收到货通知
     *  作者:kelvin
     * */
    public function action_supplierReturnsGet()
    {
        $_POST['data'] = array(

        );
        if($_POST){
            PurchaseApi::supplierReturnsGet($_POST['data']);
        }
    }
}